
class Txt:
    # 有关txt一系列处理的一个类


    # 写入txt文件
    def write1(self, txt_path, list, column):
        txt_file = open(txt_path, 'w')  # 写出符合要求的点
        list_num = len(list)
        for i in range(list_num):
            if list[i] != '':
                txt_file.write(str(list[i][column-1]) + '\n')
        txt_file.close()

    # 写出满足某种HER2等级的阈值的点
    def write_point_txt(self, txt_path, point_list, degree_list, columnX, columnY, pro):
        assert len(point_list) == len(degree_list)
        txt_file = open(txt_path, 'w')  # 写出符合要求的点
        list_num = len(point_list)
        for i in range(list_num):
            if point_list[i] != '' and degree_list[i] != '' and float(degree_list[i]) >= pro:
                txt_file.write(str(point_list[i][columnX-1]) + ',' + str(point_list[i][columnY-1]) + '\n')
        txt_file.close()

    # 读取缩略图上对应图的坐标点
    def read_point_txt(self, txt_path):
        txt_file = open(txt_path)
        all_data_in_txt = txt_file.read()
        data_list_in_txt = all_data_in_txt.split("\n")
        all_seperate_data = []

        for single_data in data_list_in_txt:
            seperate_data = single_data.split(",")
            # 因为原来代码只有x,y的坐标，所以a应该只有两个值的情况才对
            # 但生成的txt文件因为带有图片名称，所以有三个值的情况出现
            if len(seperate_data) != 2 and len(seperate_data) != 3:
                continue
                # 因为第三列是图片名称，这里用不到
            seperate_data = [int(i) for i in seperate_data[:2]]
            all_seperate_data.append(seperate_data)

        return all_seperate_data

    # 读取概率文件
    def read_pro_txt(self, txt_path):
        txt_file = open(txt_path)
        all_data_in_txt = txt_file.read()
        data_list_in_txt = all_data_in_txt.split("\n")

        list_num = len(data_list_in_txt)
        for i in range(list_num):
            if data_list_in_txt[i] == '':
                del data_list_in_txt[i]

        return data_list_in_txt